package com.zyq.service;

import com.zyq.exception.ServiceException;
import com.zyq.mapper.AlbumMapper;
import com.zyq.mapper.PictureMapper;
import com.zyq.mapper.SpuMapper;
import com.zyq.po.vo.AlbumStandardVO;
import com.zyq.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AlbumServiceImpl implements  IAlbumService{
    @Autowired
    private AlbumMapper albumMapper;

    @Autowired
    private PictureMapper pictureMapper;

    @Autowired
    private SpuMapper spuMapper;
    public AlbumServiceImpl(){
        log.debug("创建业务对象： AlbumServiceImpl");
    }

    public void delete(Long id){
        log.debug("开始处理【删除相册】的业务，参数为{}", id);
        AlbumStandardVO queryResult=albumMapper.getStandardById(id);
        if(queryResult ==null){
            String message="AlbumServiceImpl---delete()::删除相册失败，尝试访问的数据{}不存在";
            log.warn(message,id);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUNT, message);
        }

        // 检查此相册是否关联了图片
        {
            int count=pictureMapper.countPicturesByAlbumId(id);
            if(count>0){
                String message="删除相册失败！ 当前相册中任有图片!";
                log.warn(message);
                throw  new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }

        {// 检查此相册是否关联了SPU
            int count=spuMapper.countByAlbumId(id);
            if(count>0){
                String message="删除相册失败！ 当前相册任有商品相关联!";
                log.warn(message);
                throw  new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }

            // 调用Mapper对象的deleteById()方法删除相册
            log.debug("即将删除相册数据.....");
            albumMapper.deleteById(id);
            log.debug("删除相册，完成!");
        }
    }

    public AlbumStandardVO getStandardAlbumVOById(Long id) {
        log.debug("开始处理【根据id查询相册详情】的业务");
        AlbumStandardVO album=albumMapper.getStandardById(id);
        if(album==null){
            String message="获取相册详情失败，尝试访问的数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUNT,message);
        }
        return album;
    }

}






